parameters:
  buildConfig: ''
  archType: ''
  osGroup: ''
  osSubgroup: ''
  container: ''
  helixQueues: ''
  runtimeVariant: ''
  crossBuild: false
  variables: {}
  pool: ''
  targetarch: ''
  targetos: ''
  liveLibrariesBuildConfig: ''

### Crossgen-comparison job
###
### Ensure that the output of cross-architecture, e.g. x64-hosted-arm-targeting,
### crossgen matches that of native, e.g. arm-hosted-arm-targeting, crossgen.

jobs:
- template: /eng/pipelines/common/templates/runtimes/xplat-job.yml
  parameters:
    buildConfig: ${{ parameters.buildConfig }}
    archType: ${{ parameters.archType }}
    osGroup: ${{ parameters.osGroup }}
    osSubgroup: ${{ parameters.osSubgroup }}
    runtimeVariant: ${{ parameters.runtimeVariant }}
    liveLibrariesBuildConfig: ${{ parameters.liveLibrariesBuildConfig }}
    helixType: 'test/crossgen-comparison/'
    pool: ${{ parameters.pool }}
    targetos: ${{ parameters.targetos }}
    targetarch: ${{ parameters.targetarch }}

    # Compute job name from template parameters
    name: ${{ format('test_crossgen2_comparison_{0}{1}_{2}_{3}_{4}_{5}', parameters.osGroup, parameters.osSubgroup, parameters.archType, parameters.buildConfig, parameters.targetarch, parameters.targetos) }}
    displayName: ${{ format('Test crossgen2-comparison {0}{1} {2} {3} to {4} {5}', parameters.osGroup, parameters.osSubgroup, parameters.archType, parameters.buildConfig, parameters.targetarch, parameters.targetos) }}

    crossBuild: ${{ parameters.crossBuild }}

    variables:
    - ${{ if eq(variables['System.TeamProject'], 'internal') }}:
      - group: DotNet-HelixApi-Access
    - name: hostArchType
      value: x64
    - name: targetFlavor
      value: $(osGroup).$(archType).$(buildConfigUpper)
    - name: artifactsDirectory
      value: $(Build.SourcesDirectory)$(dir)artifacts
    - name: binDirectory
      value: $(artifactsDirectory)$(dir)bin
    - name: productDirectory
      value: $(binDirectory)$(dir)coreclr
    - name: workItemDirectory
      value: $(artifactsDirectory)$(dir)tests$(dir)coreclr$(dir)$(targetFlavor)$(dir)Tests$(dir)Core_Root
    - name: targetarch
      value: ${{ parameters.targetarch }}
    - name: compiler_arch_os
      value: ${{ format('{0}{1}_{2}', parameters.osGroup, parameters.osSubgroup, parameters.archType) }}
    - name: crossgencompare_build_artifact
      value: crossgen_comparison_build_${{ parameters.targetos }}_${{ parameters.targetarch }}
    - name: displayname_comparison_job
      value: ${{ format('Test crossgen2-comparison {0}{1} {2} {3} to {4} {5}', parameters.osGroup, parameters.osSubgroup, parameters.archType, parameters.buildConfig, parameters.targetarch, parameters.targetos) }}
    - ${{ if eq(parameters.targetos, 'windows') }}:
      - name: target_crossgen2_os
        value: windows
    - ${{ if eq(parameters.targetos, 'linux') }}:
      - name: target_crossgen2_os
        value: linux
    - ${{ if eq(parameters.targetos, 'osx') }}:
      - name: target_crossgen2_os
        value: osx

    - ${{ parameters.variables }}

    # Test job depends on the corresponding build job
    dependsOn:
    - ${{ format('test_crossgen2_comparison_build_{0}_{1}_Release', parameters.targetos, parameters.targetarch)}}
    - build_${{ parameters.osGroup }}${{ parameters.osSubgroup }}_${{ parameters.archType }}_${{ parameters.buildConfig }}_

    # Run all steps in the container.
    # Note that the containers are defined in platform-matrix.yml
    container: ${{ parameters.container }}
    timeoutInMinutes: 180 # 3 hrs

    steps:

    # Download product build
    - template: /eng/pipelines/common/download-artifact-step.yml
      parameters:
        unpackFolder: $(Build.SourcesDirectory)/artifacts/bin
        artifactFileName: 'Release_CoreCLR_$(osGroup)$(osSubgroup)_$(archType)_$(_BuildConfig)$(archiveExtension)'
        artifactName: 'Release_CoreCLR_$(osGroup)$(osSubgroup)_$(archType)_$(_BuildConfig)'
        displayName: 'product build'

    # Populate Core_Root
    - script: $(Build.SourcesDirectory)/src/tests/build$(scriptExt) $(buildConfig) $(archType) $(crossArg) generatelayoutonly
      displayName: Populate Core_Root

    - task: DownloadPipelineArtifact@2
      displayName: Download Preprepared crossgen inputs and expected values
      inputs:
        artifact: '$(crossgencompare_build_artifact)'
        path: '$(workItemDirectory)$(dir)prebuiltWork'

    # Send payload to Helix where the native output is generated and compared to the baseline
    - template: /eng/common/templates/steps/send-to-helix.yml
      parameters:
        DisplayNamePrefix: Run native crossgen and compare output to baseline
        osGroup: ${{ parameters.osGroup }}
        HelixSource: $(_HelixSource)
        HelixType: 'test/crossgen-comparison/'
        ${{ if eq(variables['System.TeamProject'], 'internal') }}:
          HelixAccessToken: $(HelixApiAccessToken)
        HelixTargetQueues: ${{ join(' ', parameters.helixQueues) }}
        ${{ if ne(variables['System.TeamProject'], 'internal') }}:
          Creator: $(Creator)
        WorkItemTimeout: 3:00 # 3 hours
        WorkItemDirectory: '$(workItemDirectory)'
        CorrelationPayloadDirectory: '$(Build.SourcesDirectory)/src/tests/Common/scripts'
        ${{ if ne(parameters.osGroup, 'windows') }}:
          WorkItemCommand:
            echo $(displayname_comparison_job) ;
            echo Targeting $(targetFlavor) ;
            chmod +x     $HELIX_WORKITEM_PAYLOAD/corerun;
            mkdir -p     $HELIX_WORKITEM_PAYLOAD/log;
            export CORE_ROOT=$HELIX_WORKITEM_PAYLOAD;
            python3 -u $HELIX_CORRELATION_PAYLOAD/crossgen2_comparison.py crossgen_framework
            --crossgen    $HELIX_WORKITEM_PAYLOAD/crossgen2/crossgen2.dll
            --dotnet      $HELIX_WORKITEM_PAYLOAD/corerun
            --core_root   $HELIX_WORKITEM_PAYLOAD/prebuiltWork/dlls
            --result_dir  $HELIX_WORKITEM_UPLOAD_ROOT
            --target_os   $(target_crossgen2_os)
            --target_arch $(targetarch)
            --compiler_arch_os $(compiler_arch_os);
            python3 -u $HELIX_CORRELATION_PAYLOAD/crossgen2_comparison.py compare
            --base_dir   $HELIX_WORKITEM_PAYLOAD/prebuiltWork/log
            --diff_dir   $HELIX_WORKITEM_UPLOAD_ROOT
            --testresults $HELIX_WORKITEM_ROOT/testResults.xml
            --target_arch_os $(target_crossgen2_os)_$(targetarch)
        ${{ if eq(parameters.osGroup, 'windows') }}:
          WorkItemCommand:
            echo $(displayname_comparison_job) &
            echo Targeting $(targetFlavor) &
            md        %HELIX_WORKITEM_PAYLOAD%\log &
            set CORE_ROOT=%HELIX_WORKITEM_PAYLOAD%&
            python -u %HELIX_CORRELATION_PAYLOAD%\crossgen2_comparison.py crossgen_framework
            --crossgen    %HELIX_WORKITEM_PAYLOAD%\crossgen2\crossgen2.dll
            --dotnet      %HELIX_WORKITEM_PAYLOAD%\corerun.exe
            --core_root   %HELIX_WORKITEM_PAYLOAD%\prebuiltWork\dlls
            --result_dir  %HELIX_WORKITEM_UPLOAD_ROOT%
            --target_os   $(target_crossgen2_os)
            --target_arch $(targetarch)
            --compiler_arch_os $(compiler_arch_os) &
            python -u %HELIX_CORRELATION_PAYLOAD%\crossgen2_comparison.py compare
            --base_dir   %HELIX_WORKITEM_PAYLOAD%\prebuiltWork\log
            --diff_dir   %HELIX_WORKITEM_UPLOAD_ROOT%
            --testresults %HELIX_WORKITEM_ROOT%\testResults.xml
            --target_arch_os $(target_crossgen2_os)_$(targetarch)
    # Publish log
    - task: PublishPipelineArtifact@1
      displayName: Publish log
      inputs:
        targetPath: $(Build.SourcesDirectory)/artifacts/log
        artifactName: ${{ format('Testlog_crossgen2_comparison_{0}{1}_{2}_{3}_{4}_{5}', parameters.osGroup, parameters.osSubgroup, parameters.archType, parameters.buildConfig, parameters.targetarch, parameters.targetos) }}
      continueOnError: true
      condition: always()
